<html>
<head><meta charset="utf-8"><title>Dropping extern types · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html">Dropping extern types</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239197328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239197328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239197328">(May 18 2021 at 04:41)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(extern_types)]</span><span class="w"></span>
<span class="k">extern</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">type</span> <span class="nc">T</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">drop</span><span class="p">(</span><span class="n">_</span>: <span class="nb">Box</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="c1">// What does this do?</span>
</code></pre></div>



<a name="239197745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239197745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239197745">(May 18 2021 at 04:48)</a>:</h4>
<p>Looks like right now it does nothing -- I guess that <code>size_of_val</code> is returning 0.</p>



<a name="239197765"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239197765" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239197765">(May 18 2021 at 04:49)</a>:</h4>
<p>Maybe this is a better example then</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">drop</span><span class="p">(</span><span class="n">_</span>: <span class="nc">Arc</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"> </span><span class="c1">// What does this do?</span>
</code></pre></div>



<a name="239197886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239197886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239197886">(May 18 2021 at 04:51)</a>:</h4>
<p>What's the context for the questions?</p>



<a name="239197975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239197975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239197975">(May 18 2021 at 04:52)</a>:</h4>
<p><a href="https://github.com/rust-lang/rfcs/blob/master/text/1861-extern-types.md">The RFC</a> says</p>
<blockquote>
<p>As a DST, size_of and align_of do not work, but we must also be careful that size_of_val and align_of_val do not work either, as there is not necessarily a way at run-time to get the size of extern types either. For an initial implementation, those methods can just panic, but before this is stabilized there should be some trait bound or similar on them that prevents their use statically. The exact mechanism is more the domain of the custom DST RFC, RFC 1524, and so figuring that mechanism out will be delegated to it.</p>
</blockquote>
<p>But it appears that's not yet implemented.</p>



<a name="239197980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239197980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239197980">(May 18 2021 at 04:52)</a>:</h4>
<p>I've been looking at the weird edge cases of the rust type system to figure out what a type "is", and extern types seem really weird</p>



<a name="239197988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239197988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239197988">(May 18 2021 at 04:52)</a>:</h4>
<p>It seems like these ought to be compile errors, at least when it comes to generating the drop impl, because freeing a block of memory that is attached to an extern type is likely to cause a memory error</p>



<a name="239198083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239198083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239198083">(May 18 2021 at 04:54)</a>:</h4>
<p>Yes, they are weird.  The problem is that all rust types today have a known size at runtime, but that's not the case for <code>extern type</code>.  And the full implications of that are not yet know.  (Does it need a new <code>?EvenAtRuntimeSized</code>, for example.)</p>



<a name="239198313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239198313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239198313">(May 18 2021 at 04:59)</a>:</h4>
<p>Is there any other place where "undroppable types" arise? I know that some drop impls panic but I'm not sure if there are any types which are a compile error to drop</p>



<a name="239215857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239215857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239215857">(May 18 2021 at 08:22)</a>:</h4>
<p>When doing C FFI you need to interface with <code>void*</code></p>



<a name="239215924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239215924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239215924">(May 18 2021 at 08:23)</a>:</h4>
<p>The object behind <code>void*</code> can have any runtime size</p>



<a name="239216068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239216068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239216068">(May 18 2021 at 08:24)</a>:</h4>
<p>We currently model this as <code>enum Void {}</code> (uninhabited) <code>*Void</code> . That solves only the part of the problem that prevents you from dereferencing a <code>void*</code>. This is pretty good in practice.</p>



<a name="239216154"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/239216154" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> hannahE2 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#239216154">(May 18 2021 at 08:25)</a>:</h4>
<p>It doesn't "solve" the other part that says <code>void</code> can have a "runtime" size</p>



<a name="241680751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/241680751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Demi Obenour <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#241680751">(Jun 06 2021 at 08:36)</a>:</h4>
<p>Being able to mark a type as “cannot be dropped” would be extremely useful for asynchronous code.   For instance, it would allow a <code>Future</code> to guarantee that it will still be alive when a certain callback fires.</p>



<a name="241691259"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Dropping%20extern%20types/near/241691259" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Dropping.20extern.20types.html#241691259">(Jun 06 2021 at 13:11)</a>:</h4>
<p><code>Pin</code> requires you to assert that the value to either be dropped or the memory to be leaked when calling <code>Pin::new_unchecked</code>. Combined with the not moving guarantee this means that it can be fine to write to the memory of the future if the future cooperates. As for waking up, the <code>Waker</code> is reference counted, so calling it is fine even if the future doesn't exist anymore.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>